package wqh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.DtDevice;
import wqh.domain.DtGeoBuilding;
import wqh.domain.DtSensor;
import wqh.pojo.MapRet;
import wqh.service.DtDeviceService;
import wqh.service.DtGeoBuildingService;
import wqh.service.DtSensorService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by devts on 2016/11/30.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/water/dt_sensor")
public class DtSensorJSONController {

    @Autowired
    private DtSensorService dtSensorService;

    @Autowired
    private DtDeviceService dtDeviceService;

    @Autowired
    private DtGeoBuildingService dtGeoBuildingService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody DtSensor dtSensor)
    {
        try {
            dtSensorService.insert(dtSensor);
            return MapRet.SQL_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_ERROR();
        }
    }

    @RequestMapping(value = "/getNameByDeviceId",method = RequestMethod.GET)
    public Object getNameByDeviceId(@RequestParam(value = "device_id",required = true) int device_id)
    {
        List<DtSensor> dtSensors = dtSensorService.findByDeviceId(device_id);
        if(dtSensors.size() == 0) return MapRet.DATA(null);
        List<Object> list = new ArrayList<Object>();
        for(DtSensor dtSensor: dtSensors){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id",dtSensor.getId());
            map.put("text",dtSensor.getCode());
            list.add(map);
        }
        return MapRet.DATA(list);
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll()
    {
        List<DtDevice> devices = dtDeviceService.findAll();
        if(devices.size() == 0) return MapRet.DATA(null);
        List<DtSensor> sensors = dtSensorService.findAll();
        if(sensors.size() == 0) return MapRet.DATA(null);
        List<DtGeoBuilding> geoBuildings = dtGeoBuildingService.findAllRecord();
        if(geoBuildings.size() == 0) return MapRet.DATA(null);
        List<Object> objects = new ArrayList<Object>();
        for(DtSensor sensor : sensors){
            DtDevice device = null;
            DtGeoBuilding geoBuilding = null;
            for(DtDevice dtDevice : devices){
                if(sensor.getDevice_id().equals(dtDevice.getId())){
                    device = dtDevice;
                    break;
                }
            }
            for(DtGeoBuilding dtGeoBuilding : geoBuildings){
                if(sensor.getGeo_id().equals(dtGeoBuilding.getId())){
                    geoBuilding = dtGeoBuilding;
                    break;
                }
            }
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("id",sensor.getId());
            map.put("code",sensor.getCode());
            map.put("detail",sensor.getDetail());
            if(device != null){
                map.put("device_id",device.getId());
                map.put("device_name",device.getName());
                map.put("device_ip",device.getIp());
                map.put("device_detail",device.getDetail());
                map.put("device_detail",device.getDetail());
                map.put("device_last_time",device.getLast_time());
            }else {
                map.put("device_name","无数据");
            }
            if(geoBuilding != null){
                String geoName = null;
                if(geoBuilding.getLevel() == 3){
                    geoName = geoBuilding.getName();
                    for(DtGeoBuilding geoBuilding2 : geoBuildings){
                        if(geoBuilding2.getId().equals(geoBuilding.getParent_id())){
                            geoName = geoBuilding2.getName() + "___" + geoName;
                            for(DtGeoBuilding geoBuilding1:geoBuildings){
                                if(geoBuilding1.getId().equals(geoBuilding2.getParent_id())){
                                    geoName = geoBuilding1.getName() + "___" + geoName;
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                if(geoBuilding.getLevel() == 2){
                    for(DtGeoBuilding geoBuilding1:geoBuildings){
                        if(geoBuilding1.getId().equals(geoBuilding.getParent_id())){
                            geoName = geoBuilding1.getName() + "___" + geoName;
                            break;
                        }
                    }
                }
                if(geoBuilding.getLevel() == 1){
                    geoName = geoBuilding.getName();
                }
                map.put("geo_name",geoName);
            }else{
                map.put("geo_name","无数据");
            }
            objects.add(map);
        }
        return MapRet.DATA(objects);
    }

}
